﻿using Autofac;
using Microsoft.Extensions.Logging;
using Sayook.Framework.DbCore.DbConnection;
using Sayook.Framework.DbCore.DbContext;

namespace DbCore.Template
{
    public class DbContextAutofacModule : Module
    {
        /// <summary>
        /// 加载服务组件
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<MySqlDbConnection>().As<IDbConnectionProvider>().SingleInstance();

            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .AddFilter("System", LogLevel.Warning)
                .AddFilter("DbCore.Program", LogLevel.Debug)
                .AddConsole();
            });
            builder.Register(c => loggerFactory.CreateLogger<Program>()).As(typeof(ILogger<Program>)).SingleInstance();

            builder.RegisterGeneric(typeof(CommonDbContext<>));
            builder.Register(c => loggerFactory.CreateLogger<CommonDbContext<PowerInfo>>()).As<ILogger<CommonDbContext<PowerInfo>>>().SingleInstance();
            builder.Register(c => loggerFactory.CreateLogger<CommonDbContext<BankInfo>>()).As<ILogger<CommonDbContext<BankInfo>>>().SingleInstance();

            builder.RegisterType<TranDbContext>();
            builder.Register(c => loggerFactory.CreateLogger<TranDbContext>()).As<ILogger<TranDbContext>>().SingleInstance();
        }
    }
}